import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { HttpRequest } from "../../../../service/httpRequest";
import { flyIn } from '../../../../animations/fly-in';
import { NotiService } from "../../../../service/noti.service";


declare var $:any;
@Component({
  selector: 'app-group-member',
  templateUrl: './group-member.component.html',
  styleUrls: ['./group-member.component.css'],
  animations: [
    flyIn
  ]
})
export class GroupMemberComponent implements OnInit {
  id:string = '';
  name:string = '';
  data:any = [];
  private gizwitsNotiWs:any;
  private eventType = ['device.status.kv'];

  constructor(private activeRoute:ActivatedRoute,
              private httpRequest:HttpRequest,
              private noti:NotiService) {
  }

  ngOnInit() {
    this.activeRoute.params.subscribe(
      params=> {
        this.id = params['groupId'];
        this.name = params['groupName'];
        this.getData();
      }
    );
  }



  getData() {
    this.httpRequest.sendGet('devices/bygroup', {group: this.id}).subscribe(res=> {
      this.data = res.result;
      let tdata = res.result;
      let macs = [];
      let prokey;
      for (let i = 0; i < tdata.length; i++) {
        prokey = tdata[i].id;
        let mac = tdata[i].mac;
        macs.push(mac);
      }
      if(prokey){
        this.gizwitsNotiWs = this.noti.init(prokey.split("/")[0],macs,this.eventType,this.onEvent);
        this.gizwitsNotiWs.connect();
      }

      //this.getrealtime(prokey, macs);
    }, err=> {
    });
  }

  //getrealtime(pk, macs) {
  //  if (pk != undefined) {
  //    pk = pk.split("/");
  //    let gizwitsNotiWs;
  //    let enterpriseId = '96d09b9d8d8c4086bca6b0682c60e353';
  //    let enterpriseSecret = '6e4fa5d6081e4ce4b0fa3d5caf5f5c73';
  //    let host = 'noti.gizwits.com';
  //    let port = '2016';
  //    gizwitsNotiWs = new giz.GizwitsNotiWS(enterpriseId, enterpriseSecret, host, port);
  //    gizwitsNotiWs.onConnected = this.onConnected(pk,macs,gizwitsNotiWs);
  //    gizwitsNotiWs.onSubscribed = () => console.log("完成onSubscribed!可以开始接受消息。");
  //    giz.GizwitsNotiWS.onEvent = this.onEvent;
  //    gizwitsNotiWs.onError = (value) => console.log(value.toString());
  //
  //    gizwitsNotiWs.connect();
  //  }
  //}

  //onConnected(pk,macs,eventType,gizwitsNotiWs){
  //  console.log("完成onConnected!可以开始订阅事件。");
  //
  //  let data = [{
  //    "product_key": pk,
  //    "macs": macs,
  //    "events": eventType
  //  }];
  //  if (data.length > 0) {
  //    console.log("开始订阅事件!");
  //    gizwitsNotiWs.subscribe(data);
  //  }
  //}

  //subscribe(pk,macs,gizwitsNotiWs) {
  //
  //  let eventType = ['device.status.kv'];
  //  let data = [{
  //    "product_key": pk[0],
  //    "macs": macs,
  //    "events": eventType
  //  }];
  //  //console.log(data.length());
  //  if (data.length > 0) {
  //    console.log("开始订阅事件!");
  //    //console.log(data);
  //    gizwitsNotiWs.subscribe(data);
  //  }
  //}

  onEvent(msg) {
    let tags = $('div[tag=' + msg.mac + ']');
    for (let i = 0; i < tags.length; i++) {
      let tag = tags[i];
      let key = $(tag).attr('hu');
      $(tag).text(msg.data[key].toFixed(1));

    }

    let mdtags = $('button[mdtag=' + msg.mac + ']');
    for (let i = 0; i < mdtags.length; i++) {
      let tag = mdtags[i];
      let key = $(tag).attr('hu');
      let val = msg.data[key];
      $(tag).removeClass();
      switch (key) {
        case 'RunState1_01':
          if (val == 1) {
            $(tag).addClass('btn btn-success btn-sm');
            $(tag).text('运行');
          } else {
            $(tag).text('停止');
          }
          break;
        case 'RunState1_02':
          if (val == 1) {
            $(tag).addClass('btn btn-info btn-sm');
            $(tag).text('加载');
          } else {
            $(tag).text('卸载');
          }
          break;
        case 'RunState04_10':
          if (val == 1) {
            $(tag).addClass('btn btn-success btn-sm');
          }
          break;
        case 'RunState04_12':
          if (val == 1) {
            $(tag).addClass('btn btn-warning btn-sm');
          }
          break;
        case 'RunState04_11':
          if (val == 1) {
            $(tag).addClass('btn btn-danger btn-sm');
          }
          break;
        default:
          break;
      }
      if (val == 0) {
        $(tag).addClass('btn btn-sm');
      }
    }
  }
}
